package com.open.pokergames

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.open.pokergames.base.activity.MainBaseActivity
import com.open.pokergames.conn.bean.Message
import com.open.pokergames.main.GroupMainFragment
import com.open.pokergames.main.HomeMainFragment
import com.open.pokergames.main.MineMainFragment
import com.sinothk.tab.weiXin.WxTabMenuMainAdapter
import kotlinx.android.synthetic.main.activity_main_tabs.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainActivity : MainBaseActivity() {

    private var homeMainFragment: HomeMainFragment? = null

    companion object {
        var instant: MainActivity? = null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        instant = this
    }

    override fun initTab() {
        val fragments = ArrayList<Fragment>()
        homeMainFragment = HomeMainFragment()
        fragments.add(homeMainFragment!!)
        fragments.add(GroupMainFragment())
        fragments.add(MineMainFragment())

        val mainAdapter = WxTabMenuMainAdapter(supportFragmentManager, alphaIndicator, fragments)
        mViewPager!!.adapter = mainAdapter
        mViewPager!!.offscreenPageLimit = fragments.size - 1

        // 监听滑动，动态显示TitleBar内容
        mViewPager!!.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
//                if (position == 0 || position == 3) {
//                    ImmersionBar.with(this@MainActivity)
//                        .statusBarDarkFont(false).init()
//
////                    if (position == 3) {
////                        // 需要登录项
////                        if (!LocalCache.getAutoLogin()) {
////                            // 登录检测
////                            XUtils.intent().openActivity(this@MainActivity, LoginActivity::class.java)
////                                .start()
////                            return
////                        }
////                    }
//                } else {
//                    ImmersionBar.with(this@MainActivity)
//                        .statusBarDarkFont(true).init()
//                }
            }
        })

        alphaIndicator.setViewPager(mViewPager)
    }

//    override fun onClick(v: View?) {
//        when (v) {
//            gotoRoomBtn -> {
//                XUtils.intent().openActivity(this@MainActivity, RoomListActivity::class.java)
//                    .start()
//            }
//        }
//    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun msgCallback(message: Message?) {

//        if (XUtils.string().isEmpty(message)) return
//
//        var currInfo = resultInfoTv.text
//
//        currInfo = "$currInfo \n $message"
//
//        resultInfoTv.text = currInfo
    }

    override fun updateHomeTop() {
        homeMainFragment?.updateHomeTop()
    }

    public fun selectTab(i: Int) {
        alphaIndicator.setTabCurrenItem(i)
    }
}